package com.jzo2o.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.api.customer.dto.response.AddressBookResDTO;
import com.jzo2o.common.model.CurrentUserInfo;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.common.utils.CollUtils;
import com.jzo2o.common.utils.JwtTool;
import com.jzo2o.customer.enums.CertificationStatusEnum;
import com.jzo2o.customer.mapper.AddressBookMapper;
import com.jzo2o.customer.mapper.CommonUserMapper;
import com.jzo2o.customer.model.domain.AddressBook;
import com.jzo2o.customer.model.dto.request.AddressBookPageQueryReqDTO;
import com.jzo2o.customer.model.dto.request.AddressBookUpsertReqDTO;
import com.jzo2o.customer.service.IAddressBookService;
import com.jzo2o.mvc.utils.ResponseUtils;
import com.jzo2o.mysql.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.Response;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 地址薄 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-07-06
 */
@Service
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook> implements IAddressBookService {

    @Resource
    private JwtTool jwtTool;
    @Resource
    private AddressBookMapper addressBookMapper;
    @Autowired
    private CommonUserMapper commonUserMapper;


    @Override
    public List<AddressBookResDTO> getByUserIdAndCity(Long userId, String city) {

        List<AddressBook> addressBooks = lambdaQuery()
                .eq(AddressBook::getUserId, userId)
                .eq(AddressBook::getCity, city)
                .list();
        if (CollUtils.isEmpty(addressBooks)) {
            return new ArrayList<>();
        }
        return BeanUtils.copyToList(addressBooks, AddressBookResDTO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insert(AddressBookUpsertReqDTO dto, String token) {

        String[] split = dto.getLocation().split(",");
        CurrentUserInfo currentUserInfo = jwtTool.parseToken(token);
        AddressBook addressBook = new AddressBook();
        BeanUtil.copyProperties(dto, addressBook);
        addressBook.setUserId(currentUserInfo.getId());
        addressBook.setCreateTime(LocalDateTime.now());
        addressBook.setCreateBy(currentUserInfo.getId());
        addressBook.setLon(Double.valueOf(split[0]));
        addressBook.setLat(Double.valueOf(split[1]));

        //判断当前地址是否设置了默认地址
        if (dto.getIsDefault() == CertificationStatusEnum.PROGRESSING.getStatus()) {
            //被以前的默认地址修改为普通地址
            addressBookMapper.update(new AddressBook().setIsDefault(CertificationStatusEnum.INIT.getStatus())
                    , new LambdaQueryWrapper<AddressBook>()
                            .eq(AddressBook::getUserId, currentUserInfo.getId())
                            .eq(AddressBook::getIsDefault, CertificationStatusEnum.PROGRESSING.getStatus()));
        }
        addressBookMapper.insert(addressBook);
    }

    @Override
    public PageResult<AddressBook> page(AddressBookPageQueryReqDTO dto,String token) {
        CurrentUserInfo currentUserInfo = jwtTool.parseToken(token);
        Page<AddressBook> page = PageUtils.parsePageQuery(dto,AddressBook.class);
        LambdaQueryWrapper<AddressBook> queryWrapper = Wrappers.<AddressBook>lambdaQuery()
                .eq(AddressBook::getUserId,currentUserInfo.getId());
        Page<AddressBook> addressBookPage = addressBookMapper.selectPage(page, queryWrapper);
        return PageUtils.toPage(addressBookPage,AddressBook.class);
    }


}
